package com.webproject.action.houseManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.houseManager.Building;
import com.webproject.service.houseManager.BuildingManagerService;

public class BuildingManagerAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Page mpage;
	private List<Building> buildings;
	public List<Building> getBuildings() {
		return buildings;
	}
	public void setBuildings(List<Building> buildings) {
		this.buildings = buildings;
	}
	private BuildingManagerService buildingManagerService;
	public Page getMpage() {
		return mpage;
	}
	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}
	public void setBuildingManagerService(
			BuildingManagerService buildingManagerService) {
		this.buildingManagerService = buildingManagerService;
	}
	public String queryBuilding(){
		mpage=getPage();
		buildingManagerService.queryBuilding(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String queryBuildingByJson(){
		buildings=buildingManagerService.queryBuilding();
		return SUCCESS;
	}
	public String editBuilding(){
		Building building=getBuilding();
		if(getOper().equals("add")){
			buildingManagerService.saveBuilding(building);
		}else if (getOper().equals("edit")){
			buildingManagerService.updateBuilding(building);
		}else if(getOper().equals("del")){
			buildingManagerService.deleteBuilding(building);
		}
		return SUCCESS;
	}
	private Building getBuilding(){
		Building building=new Building();
		String id=getRequest().getParameter("id");
		String number=getRequest().getParameter("number");
		String position=getRequest().getParameter("position");
		String totalHeight=getRequest().getParameter("totalHeight");
		String buildingTime=getRequest().getParameter("buildingTime");
		String useTime=getRequest().getParameter("useTime");
		building.setId(id);
		building.setNumber(number);
		building.setPosition(position);
		building.setTotalHeight(isNotEmpty(totalHeight)?Integer.parseInt(totalHeight):0);
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		try {
			building.setBuildingTime(isNotEmpty(buildingTime)?dateFormat.parse(buildingTime):null);
			building.setUseTime(isNotEmpty(useTime)?dateFormat.parse(useTime):null);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return building;
	}
}
